package com.key.oa.activity;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.key.keyleaderoa.R;
import com.key.oa.common.Json;
import com.key.oa.http.HttpUncheck;
import com.key.oa.util.Util;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

public class BackgroundService extends Service {
	
	private Timer timer = null;
	
	private AlarmManager alarm = null;
	
	private NotificationManager manager = null;
	
	@Override
	public void onCreate() {
		super.onCreate();		
		timer = new Timer(true);
		timer.schedule(task, 5000, 15*60*1000);
		//initTimer();
	}

	private void initTimer() {

		PendingIntent sender = PendingIntent.getBroadcast(this, 0, 
				new Intent(this, MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK);
		
	    long now = SystemClock.elapsedRealtime();
	    alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
	    alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, now, 15*60*1000, sender);
	}
	
	public TimerTask task = new TimerTask() {
		@Override
		public void run() {  
			int size = HttpSend();
			if( size > 0 ) {
				addNewNotificaction(size);
			}
	    }  
	};
	
	private int HttpSend() {
		
		int size = 0;
		try {
			String uri = Util.serverdata.getUriUnchecked();
			String result = HttpUncheck.get(uri);
			if( result == null || result.length() == 0 ) {
				return 0;
			}
			
			List< Map<String,String> > parseResult = Json.parse(result);
			if( parseResult == null ) {
				throw new Exception("Parse the data error.");
			}
			
			size = parseResult.size();
			
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("oa", "error", e);
		}
		return size;
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}
	
	private void addNewNotificaction( int size ) {  
        
		if( manager != null ) {
			manager.cancel(0);
		}
		
		manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);  
        Notification notification = new Notification();
        notification.icon = R.drawable.ic_launcher;
        notification.tickerText = "APM系统";
        notification.defaults = Notification.DEFAULT_SOUND;  
        notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;  
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        notification.setLatestEventInfo(this, "新内容提示:", "新增加的流程申请数量: "+Integer.toString(size)+" 条", pendingIntent);  
        manager.notify(0, notification); 
    }
}
